package com.shapawan.tourism.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shapawan.tourism.entity.Location;
import com.shapawan.tourism.entity.LocationType;
import com.shapawan.tourism.mapper.LocationMapper;
import com.shapawan.tourism.service.LocationService;
import com.shapawan.tourism.service.LocationTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 位置信息Service实现类
 */
@Service
public class LocationServiceImpl extends ServiceImpl<LocationMapper, Location> implements LocationService {

    @Autowired
    private LocationTypeService locationTypeService;

    @Override
    public IPage<Location> pageList(Page<Location> page, Integer typeId, String keyword) {
        LambdaQueryWrapper<Location> queryWrapper = new LambdaQueryWrapper<>();
        
        // 如果指定了类型ID，则按类型查询
        if (typeId != null && typeId > 0) {
            queryWrapper.eq(Location::getTypeId, typeId);
        }
        
        // 如果有关键词，则按名称或地址模糊查询
        if (StringUtils.hasText(keyword)) {
            queryWrapper.and(wrapper -> 
                wrapper.like(Location::getName, keyword)
                       .or()
                       .like(Location::getAddress, keyword)
            );
        }
        
        // 按排序和ID排序
        queryWrapper.orderByAsc(Location::getSortOrder)
                   .orderByDesc(Location::getId);
                   
        // 查询分页数据
        IPage<Location> pageResult = page(page, queryWrapper);
        
        // 补充位置类型信息
        pageResult.getRecords().forEach(this::fillLocationType);
        
        return pageResult;
    }

    @Override
    public List<Location> getRecommendLocations() {
        LambdaQueryWrapper<Location> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Location::getStatus, 1)
                   .eq(Location::getIsRecommend, 1)
                   .orderByAsc(Location::getSortOrder);
        
        List<Location> locationList = list(queryWrapper);
        locationList.forEach(this::fillLocationType);
        
        return locationList;
    }

    @Override
    public List<Location> getLocationsByType(Integer typeId) {
        LambdaQueryWrapper<Location> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Location::getStatus, 1)
                   .eq(Location::getTypeId, typeId)
                   .orderByAsc(Location::getSortOrder);
        
        List<Location> locationList = list(queryWrapper);
        locationList.forEach(this::fillLocationType);
        
        return locationList;
    }

    @Override
    public Location getLocationDetail(Integer id) {
        Location location = getById(id);
        if (location != null) {
            fillLocationType(location);
        }
        return location;
    }

    @Override
    public void increaseViewCount(Integer id) {
        Location location = getById(id);
        if (location != null) {
            location.setViewCount(location.getViewCount() + 1);
            updateById(location);
        }
    }
    
    /**
     * 填充位置类型信息
     */
    private void fillLocationType(Location location) {
        if (location != null && location.getTypeId() != null) {
            LocationType locationType = locationTypeService.getById(location.getTypeId());
            location.setLocationType(locationType);
        }
    }
} 